package com.bbs.controller;

import com.bbs.dto.CommendQuery;
import com.bbs.dto.CommentAdd;
import com.bbs.pojo.Result;
import com.bbs.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class CommentController {
    @Autowired
    ICommentService commentService;

    /**
     * 添加评论
     * @param commentAdd
     * @return
     * @throws Exception
     */
    @PostMapping("/comments")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> addComment(@RequestBody CommentAdd commentAdd) throws Exception {
        return Result.build(commentService.addComment(commentAdd));
    }

    /**
     * 获取评论
     * @param commendQuery
     * @return
     */
    @PostMapping("/comments/query")
    public Result<Object> getComment(@RequestBody CommendQuery commendQuery){
        return Result.withData(commentService.getComments(commendQuery));
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/comments/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> deleteComment(@PathVariable("id") String id){
        return Result.build(commentService.deleteComment(id));
    }
}
